package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.BookService.impl.UserService;
import com.example.demo.commom.Result;
import com.example.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * 读者管理
 */
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Resource
    UserService userService;

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        log.info("用户注册：{}", user);
        return userService.insert(user);
    }

    /**
     * 用户登录
     * 注解 @CrossOrigin 作用是允许跨域访问该登录接口
     *
     * @param user
     * @return
     */
    @CrossOrigin
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        log.info("用户登录：{}", user);
        return userService.userLogin(user);
    }

    /**
     * 设置默认密码
     *
     * @param user
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody User user) {
        log.info("保存默认密码：{}", user);
        userService.setDefaultPassword(user);
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param id
     * @param password2
     * @return
     */
    @PutMapping("/password")
    public Result<?> update(@RequestParam Integer id,
                            @RequestParam String password2) {
        log.info("修改密码，id：{}", id);
        userService.setPassword(id, password2);
        return Result.success();
    }

    /**
     * 修改读者个人信息
     *
     * @param user
     * @return
     */
    @PutMapping
    public Result<?> password(@RequestBody User user) {
        log.info("修改读者个人信息：{}", user);
        userService.updateById(user);
        return Result.success();
    }

    /**
     * 根据id批量删除读者用户
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        log.info("根据id批量删除读者用户：{}", ids);
        userService.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 根据id删除读者用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        log.info("根据id删除用户：{}", id);
        userService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据用户昵称分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param search   用户昵称
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        log.info("根据用户姓名分页查询，页码：{},页数：{},姓名：{}", pageNum, pageSize, search);
        Page<User> userPage = userService.findPage1(pageNum, pageSize, search);
        return Result.success(userPage);
    }

    /**
     * 管理员分页查询和条件查询读者用户信息
     * 只查询身份为读者的账号，不查询管理员的账号
     *
     * @param pageNum  页码数
     * @param pageSize 总页数
     * @param search1  用户编号
     * @param search2  用户昵称
     * @param search3  用户电话
     * @param search4  用户地址
     * @return
     */
    @GetMapping("/usersearch")
    public Result<?> findPage2(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(defaultValue = "") String search1,
                               @RequestParam(defaultValue = "") String search2,
                               @RequestParam(defaultValue = "") String search3,
                               @RequestParam(defaultValue = "") String search4) {
        log.info("管理员分页查询读者管理数据，页码：{}, 页数：{},用户编号：{},用户昵称：{},用户电话：{},用户地址：{}", pageNum, pageSize, search1, search2, search3, search4);
        Page<User> userPage = userService.findPage2(pageNum, pageSize, search1, search2, search3, search4);
        return Result.success(userPage);
    }
}
